// packhunter - A cowardly creature. When it sights party, it stalks them from a distance
// until there are enough friendly creatures nearby. At that point, it attacks.
// Always sets own courage to low.
// Cell 0 - The number of creatures which must be nearby to go mad. Defaults to 3.
// Cell 1,2 - The number of the SDF to increment when killed. No effect if left at 0

begincreaturescript;

variables;

short have_sighted = 0;
short gone_rogue = 0;
short char_num;
short risk_tolerance;
short gave_warn = 0;

body;

beginstate INIT_STATE;
	set_courage(ME,30);
	
	risk_tolerance = 3;
	if (get_memory_cell(0) != 0)
		risk_tolerance = get_memory_cell(0);
	break;

beginstate DEAD_STATE;
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		inc_flag(get_memory_cell(1),get_memory_cell(2),1);
break;

beginstate START_STATE; 
	// Do we go rogue?
	if ((gone_rogue == 0) && (friends_nearby(6) >= risk_tolerance)) {
		print_named_str(ME,"snarls and charges!");
		//print_nums(my_number(),friends_nearby(6),0);
		broadcast_char_message(10,51,1);
		gone_rogue = 1;
		}
		
	if ((gone_rogue == 0) && (my_current_message() == 51)) {
		//print_str("got rogue mess");
		set_act_at_dist(ME,1);
		gone_rogue = 1;
		}
	
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if ((dist_to_char(get_target()) <= 16) && (gone_rogue > 0))
			set_state(3);
			else set_foe_target(ME,-1);
		}
		else set_foe_target(ME,random_party_member());
	
	if (get_foe_target(ME,8,0)) {
		have_sighted = 1;
		if (gave_warn == 0) {
			gave_warn = 1;
			print_named_str(ME,"watches you warily.");
			}	
		if (gone_rogue > 0) {
			do_attack();
			set_state(3);
			}
		}
	if (who_shot_me() >= 0) {
		have_sighted = 1;
		if (gone_rogue > 0) {
			set_foe_target(ME,who_shot_me());
			do_attack();
			set_state(3);
			}
			else maintain_dist_to_char(ME,who_shot_me(),12);
		}
	if (have_sighted > 0) {
		char_num = get_nearest_hate_char(12);
		if (get_health(ME) < get_max_health(ME)) {
			if (char_num >= 0) {
				if (dist_to_char(char_num) < 6)
					maintain_dist_to_char(ME,char_num,10);
				}
				else if (pc_num() >= 0)
					maintain_dist_to_char(ME,pc_num(),10);
			}
			else {
				if (char_num >= 0) {
					if (dist_to_char(char_num) < 6)
						maintain_dist_to_char(ME,char_num,4);
					}
					else if (pc_num() >= 0)
						maintain_dist_to_char(ME,pc_num(),4);
				}
			
		}
		else if (my_dist_from_start() >= 6) {
			if (get_ran(1,1,100) < 40) 
				return_to_start(ME,1);
			}
			else fidget(ME,6);

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;
